<!--
  项目流程-投决会申请
  @author YangMiao 1939724848@qq.com
  @date 2022/3/29 17:47
  @version 1.0.0
-->
<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm" @submit.native.prevent>
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('投资关系')" prop="investmentRelationship">
            <dict-select v-model="form.investmentRelationship" dictionary="project_application_investment_relationship" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('本轮其他融资方')" prop="otherFinanciersInThisRound">
            <el-input v-model="form.otherFinanciersInThisRound" :placeholder="$t('本轮其他融资方')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('转让方')" prop="transferor">
            <el-input v-model="form.transferor" :placeholder="$t('转让方')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目介绍方承诺')" prop="projectIntroducerSCommitment">
            <el-input v-model="form.projectIntroducerSCommitment" :placeholder="$t('项目介绍方承诺')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('其他主体要求')" prop="otherSubjectRequirements">
            <el-input v-model="form.otherSubjectRequirements" :placeholder="$t('其他主体要求')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <div class="area-header" style="margin-top: 20px;">
            <span class="area-header-title">{{ $t('投资金额') }}</span>
          </div>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目投前估值（万）')" prop="projectPreInvestmentValuation">
            <Money v-model="form.projectPreInvestmentValuation" :placeholder="$t('项目投前估值（万）')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目投后估值（万）')" prop="postInvestmentValuation">
            <Money v-model="form.postInvestmentValuation" :placeholder="$t('项目投后估值（万）')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('老股本轮融资总金额（万）')" prop="totalAmountOfFinancing">
            <Money v-model="form.totalAmountOfFinancing" :placeholder="$t('本轮融资总金额（万）')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('新股本轮融资总金额（万）')" prop="newTotalAmountOfFinancing">
            <Money v-model="form.newTotalAmountOfFinancing" :placeholder="$t('新股本轮融资总金额（万）')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('老股我方投资金额（万）')" prop="amountOfOurInvestment">
            <Money v-model="form.amountOfOurInvestment" :placeholder="$t('老股我方投资金额（万）')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('新股我方投资金额（万）')" prop="newAmountOfOurInvestment">
            <Money v-model="form.newAmountOfOurInvestment" :placeholder="$t('新股我方投资金额（万）')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('我方持股比例（%）')" prop="ourShareholdingRatio">
            <Money v-model="form.ourShareholdingRatio" :placeholder="$t('我方持股比例（%）')" unit="rate" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('打款时间')" prop="paymentTime">
            <el-date-picker
              v-model="form.paymentTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('打款时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <div class="area-header" style="margin-top: 20px;">
            <span class="area-header-title">{{ $t('对赌安排') }}</span>
          </div>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('业绩指标')" prop="performanceIndicators">
            <el-input v-model="form.performanceIndicators" :placeholder="$t('业绩指标')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('上市安排')" prop="listingArrangements">
            <el-input v-model="form.listingArrangements" :placeholder="$t('上市安排')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('估值保障')" prop="valuationGuarantee">
            <el-input v-model="form.valuationGuarantee" :placeholder="$t('估值保障')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('回购条款')" prop="buybackClause">
            <el-input v-model="form.buybackClause" :placeholder="$t('回购条款')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <div class="area-header" style="margin-top: 20px;">
            <span class="area-header-title">{{ $t('特殊权利') }}</span>
          </div>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('是否有一票否决权')" prop="oneVoteVeto">
            <dict-select v-model="form.oneVoteVeto" dictionary="sys_yes_no" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('我方是否有董事会席位')" prop="boardSeats">
            <dict-select v-model="form.boardSeats" dictionary="boardSeats" />
          </el-form-item>
        </el-col>
        <el-col v-if="form.boardSeats === 'Y'" :span="12">
          <el-form-item :label="$t('董事会席位是否有一票否决权')" prop="doesTheBoardSeatHaveAVeto">
            <dict-select v-model="form.doesTheBoardSeatHaveAVeto" dictionary="sys_yes_no" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('监事')" prop="oversee">
            <dict-select v-model="form.oversee" dictionary="sys_yes_no" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('优先认购权')" prop="preEmptiveRights">
            <dict-select v-model="form.preEmptiveRights" dictionary="sys_yes_no" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('优先购买权')" prop="rightOfFirstRefusal">
            <dict-select v-model="form.rightOfFirstRefusal" dictionary="sys_yes_no" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('共同出售权')" prop="jointSaleRights">
            <dict-select v-model="form.jointSaleRights" dictionary="sys_yes_no" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('领售权')" prop="rightOfSale">
            <dict-select v-model="form.rightOfSale" dictionary="sys_yes_no" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('反稀释权')" prop="antiDilutionRights">
            <dict-select v-model="form.antiDilutionRights" dictionary="sys_yes_no" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('优先清算权')" prop="liquidationPreference">
            <dict-select v-model="form.liquidationPreference" dictionary="sys_yes_no" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('知情权和检查权')" prop="rightToKnow">
            <dict-select v-model="form.rightToKnow" dictionary="sys_yes_no" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('最惠国待遇')" prop="mostFavoredNationTreatment">
            <dict-select v-model="form.mostFavoredNationTreatment" dictionary="sys_yes_no" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('召开时间')" prop="conveningTime">
            <el-date-picker
              v-model="form.conveningTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('召开时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('召开地点')" prop="venue">
            <el-input v-model="form.venue" :placeholder="$t('召开地点')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>

        <formInnerTable
          title="投资主体"
          :data-list="form.voteSubjects"
          :init-data="initData"
        >
          <el-table-column :label="$t('主体类型')">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949; margin-right: 4px">*</i>
              <span>{{ $t("主体类型") }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'investmentSubjects.' + scope.$index + '.bodyType'"
                :rules="rules.bodyType"
              >
                <dictSelect
                  v-model="scope.row.bodyType"
                  :clearable="true"
                  dictionary="deal_investment_type"
                />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('主体名称')">
            <template slot="header" slot-scope="{}">
              <i style="color: #ff4949; margin-right: 4px">*</i>
              <span>{{ $t("主体名称") }}</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'investmentSubjects.' + scope.$index + '.principalName'"
                :rules="rules.principalName"
              >
                <MainBodySelect
                  v-model="scope.row.principalName"
                  :parameters="scope.row.bodyType"
                />
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column :label="$t('备注')">
            <template slot-scope="scope">
              <el-form-item>
                <el-input v-model="scope.row.remark" type="textarea" show-word-limit :maxlength="2000" />
              </el-form-item>
            </template>
          </el-table-column>
        </formInnerTable>

      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="voteInfo" :btn-show="!otherBtnHide" />
    </el-form>
  </div>
</template>

<script>
import formInnerTable from '@/views/saas/yueyin/deal/component/formInnerTable'
import { getDealName } from '@/api/base/deal/dealBase'
import MainBodySelect from '@/components/MainBodySelect'
// import {
//   getSysYueyinProjectApplicationInfo,
//   saveSysYueyinProjectApplicationInfo
// } from '@/api/sass/yueyin/deal/projectApplyInfo/sysYueyinProjectApplicationInfo'
import {
  getByWfNodeId,
  getSysYueyinProjectVoteInfo,
  saveSysYueyinProjectVoteInfo
} from '@/api/sass/yueyin/deal/vote/sysYueyinProjectVoteInfo'

export default {
  name: 'VoteInfo',
  components: {
    formInnerTable,
    MainBodySelect
  },
  data() {
    return {
      // 表单参数
      form: {
        id: undefined,
        dealId: this.$route.query.id,
        investmentRelationship: undefined,
        bodyType: undefined,
        otherFinanciersInThisRound: undefined,
        transferor: undefined,
        projectIntroducerSCommitment: undefined,
        otherSubjectRequirements: undefined,
        projectPreInvestmentValuation: undefined,
        postInvestmentValuation: undefined,
        totalAmountOfFinancing: undefined,
        amountOfOurInvestment: undefined,
        ourShareholdingRatio: undefined,
        paymentTime: undefined,
        performanceIndicators: undefined,
        listingArrangements: undefined,
        valuationGuarantee: undefined,
        buybackClause: undefined,
        oneVoteVeto: undefined,
        boardSeats: undefined,
        doesTheBoardSeatHaveAVeto: undefined,
        oversee: undefined,
        preEmptiveRights: undefined,
        rightOfFirstRefusal: undefined,
        jointSaleRights: undefined,
        rightOfSale: undefined,
        antiDilutionRights: undefined,
        liquidationPreference: undefined,
        rightToKnow: undefined,
        mostFavoredNationTreatment: undefined,
        conveningTime: undefined,
        venue: undefined,
        actId: undefined,
        actStatus: undefined,
        actTitle: undefined,
        remark: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        newAmountOfOurInvestment: undefined,
        newTotalAmountOfFinancing: undefined,
        voteSubjects: [],
        delFlag: undefined
      },
      // 表单校验
      rules: {
        investmentRelationship: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        otherFinanciersInThisRound: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        projectIntroducerSCommitment: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        otherSubjectRequirements: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        projectPreInvestmentValuation: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        postInvestmentValuation: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        totalAmountOfFinancing: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        newTotalAmountOfFinancing: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        amountOfOurInvestment: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        newAmountOfOurInvestment: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        ourShareholdingRatio: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        paymentTime: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        performanceIndicators: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        listingArrangements: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        valuationGuarantee: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        buybackClause: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        oneVoteVeto: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        boardSeats: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        doesTheBoardSeatHaveAVeto: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        oversee: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        preEmptiveRights: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        rightOfFirstRefusal: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        jointSaleRights: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        rightOfSale: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        antiDilutionRights: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        liquidationPreference: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        rightToKnow: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        mostFavoredNationTreatment: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        conveningTime: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
        venue: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
      },
      // 遮罩层
      loading: false,
      // 操作按钮默认不展示
      buttonShow: true,
      // 是否隐藏保存按钮
      otherBtnHide: false,
      initData: {
        bodyType: '',
        principalName: '',
        remark: ''
      }
    }
  },
  created() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      console.log('data', data)
      this.loading = true
      if (data != null && data.id !== undefined) {
        getSysYueyinProjectVoteInfo(data.id).then(response => {
          this.form = response.data
          if (this.form === null) {
            this.reset()
          }
          if (this.form.investmentSubjects === undefined || this.form.investmentSubjects === null) {
            this.form.investmentSubjects = []
          }
          this.loading = false
        })
      } else {
        // 查询最新的立项申请数据
        getByWfNodeId(this.$route.query.id).then(result => {
          if (result.data === undefined || result.data === null) {
            this.reset()
          } else {
            this.form = result.data
          }
          // 默认带出流程中必须要传的附件
          if (data !== null) {
            this.form.fileList = data.fileList
          }
          this.loading = false
        })
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        dealId: this.$route.query.id,
        investmentRelationship: undefined,
        bodyType: undefined,
        otherFinanciersInThisRound: undefined,
        transferor: undefined,
        projectIntroducerSCommitment: undefined,
        otherSubjectRequirements: undefined,
        projectPreInvestmentValuation: undefined,
        postInvestmentValuation: undefined,
        totalAmountOfFinancing: undefined,
        amountOfOurInvestment: undefined,
        ourShareholdingRatio: undefined,
        paymentTime: undefined,
        performanceIndicators: undefined,
        listingArrangements: undefined,
        valuationGuarantee: undefined,
        buybackClause: undefined,
        oneVoteVeto: undefined,
        boardSeats: undefined,
        doesTheBoardSeatHaveAVeto: undefined,
        oversee: undefined,
        preEmptiveRights: undefined,
        rightOfFirstRefusal: undefined,
        jointSaleRights: undefined,
        rightOfSale: undefined,
        antiDilutionRights: undefined,
        liquidationPreference: undefined,
        rightToKnow: undefined,
        mostFavoredNationTreatment: undefined,
        conveningTime: undefined,
        venue: undefined,
        actId: undefined,
        actStatus: undefined,
        actTitle: undefined,
        remark: undefined,
        createBy: undefined,
        updateBy: undefined,
        createTime: undefined,
        updateTime: undefined,
        newAmountOfOurInvestment: undefined,
        newTotalAmountOfFinancing: undefined,
        voteSubjects: [],
        delFlag: undefined
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm(type) {
      // 关闭form页面遮罩层
      this.loading = false
      if (type !== 'actSave') {
        // 关闭form页面
        this.$parent.$parent.formOpen = false
        // 刷新list页面
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function(type) {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            // 保存数据
            saveSysYueyinProjectVoteInfo(this.form).then(response => {
              console.log('type', type)
              console.log('保存结果', response)
              if (response.code === 200) {
                // 点击的按钮是保存并启动流程
                if (type === 'process') {
                  console.log('启动流程')
                  // 设置数据id方式启动流程后取消并再次启动会创建多条记录
                  this.form.id = response.data.id
                  // 启动流程
                  this.startProcess(response.data.id)
                  // 点击的仅保存
                } else {
                  console.log('仅保存')
                  this.msgSuccess(this.$t('保存成功'))
                  // 关闭form页面
                  this.closeForm(type)
                }
                this.form.id = response.data.id
                console.log('返回id', response.data.id)
                resolve(response.data.id)
              } else {
                this.msgError(response.msg)
                this.loading = false
                reject(response)
              }
            }).catch(() => {
              this.loading = false
            })
          } else {
            reject()
            this.locationError(document)
          }
        })
      })
    },
    // 启动流程
    startProcess: async function(dataId) {
      const dealName = await getDealName(this.$route.query.id)
      this.loading = false
      // 启动流程时候传入的参数
      const processData = {}
      // 待办标题
      processData.title = '「' + dealName + '」' + ':投决会申请'
      // 业务表名
      processData.dataTable = 'sys_yueyin_project_vote_info'
      console.log('业务id', dataId)
      // 业务ID
      processData.dataId = dataId
      // 业务表单地址(待办页面需要打开的页面)
      processData.formPath = '@/views/saas/yueyin/deal/voteInfo'
      processData.viewPath = '@/views/saas/yueyin/deal/voteInfo'
      // 菜单标识(根据菜单标识判断的走哪个流程)
      processData.menuKey = 'sys_yueyin_project_vote_info'
      this.startProcessByMenu(processData, () => {
        this.form.actStatus = 'D'
        this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
        this.closeForm()
      })
    }

  }
}
</script>

<style lang="scss" scoped>

</style>
